package com.api.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.api.entity.PageVo;
import com.api.entity.Result;
import com.api.entity.Roles;
import com.api.service.RoleModulesService;
import com.api.service.RolesService;
import com.api.service.UserRolesService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@Api("角色的增删改查操作")
/* @RequestMapping("/roles") */
@ApiIgnore
public class RolesController {

	@Autowired
	private RolesService rolesService;
	@Autowired
	private RoleModulesService roleModulesService;
	@Autowired
	private UserRolesService userRolesService;
	@ApiOperation("获取所有的角色/分页")
	@PostMapping("/GetRolesAll")	//GetRolesLayUI
	public Object findAllRoles(@ApiParam("分页Page")@RequestParam("page") int page,@ApiParam("分页limit")@RequestParam("limit") int limit,HttpServletRequest request,@ApiParam("Token 密钥")@RequestParam("token") String token) {
		
		String sessionToken = (String) request.getSession().getAttribute("token");
		if(sessionToken==""||sessionToken==null) {
			Result result = new Result("您的Token已过期，请重新获取并登录",0,false);
			return result;
		}else if(sessionToken.equals(token)) {
			PageVo pageVo = new PageVo();
			pageVo.setPage(page);
			pageVo.setPageSize(limit);
			List<Roles> list = rolesService.findAllRoles(pageVo);
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("code", 0);
			map.put("count",list.size());
			map.put("data", list);
			map.put("message", 1);
			return map;
		}else {
			Result result = new Result("请核实 Token 是否正确","",false);
			return result;
		}
	}
	@ApiOperation("删除角色")
	@PostMapping("/DeleteRole")
	public Object deleteRoles(@ApiParam("角色id")@RequestParam("roleId") String roleId,HttpServletRequest request,@ApiParam("Token 密钥")@RequestParam("token") String token) {
		
		String sessionToken = (String) request.getSession().getAttribute("token");
		if(sessionToken==""||sessionToken==null) {
			Result result = new Result("您的Token已过期，请重新获取并登录",0,false);
			return result;
		}else if(sessionToken.equals(token)) {
			int i = rolesService.deleteRoles(roleId);
			if(i > 0) {
				userRolesService.deleteAllUsersRolesByrid(roleId);
				roleModulesService.deleteRoleModulesByRid(roleId);
				Result result = new Result("删除成功",3,true);
				return result;
			}else {
				Result result = new Result("删除失败",3,false);
				return result;
			}
		}else {
			Result result = new Result("请核实 Token 是否正确",1,false);
			return result;
		}
	}
	@ApiOperation("创建角色")
	@PostMapping("/CreateRole")
	public Object addRoles(@ApiParam("角色名称")@RequestParam("name") String name,HttpServletRequest request,@ApiParam("Token 密钥")@RequestParam("token") String token) {
		
		String sessionToken = (String) request.getSession().getAttribute("token");
		if(sessionToken==""||sessionToken==null) {
			Result result = new Result("您的Token已过期，请重新获取并登录",0,false);
			return result;
		}else if(sessionToken.equals(token)) {
			String id16 = id16();
			Roles roles = new Roles();
			roles.setId(id16);
			roles.setName(name);
			int i = rolesService.addRoles(roles);
			if(i > 0) {
				Result result = new Result("添加成功","",true);
				return result;
			}else {
				Result result = new Result("添加失败","",false);
				return result;
			}
		}else {
			Result result = new Result("请核实 Token 是否正确","",false);
			return result;
		}
	}
	@ApiOperation("修改角色信息")
	@PostMapping("/UpdateRole")
	public Object updateRolesByid(@ApiParam("角色id")@RequestParam("roleId") String roleId,@ApiParam("角色昵称")@RequestParam("name") String name,HttpServletRequest request,@ApiParam("Token 密钥")@RequestParam("token") String token) {
		
		String sessionToken = (String) request.getSession().getAttribute("token");
		if(sessionToken==""||sessionToken==null) {
			Result result = new Result("您的Token已过期，请重新获取并登录",0,false);
			return result;
		}else if(sessionToken.equals(token)) {
			Roles roles = new Roles();
			roles.setId(roleId); 
			roles.setName(name);
			int i = rolesService.updateRolesByid(roles);
			if(i > 0) {
				Result result = new Result("修改成功","",true);
				return result;
			}else {
				Result result = new Result("修改失败","",false);
				return result;
			}
		}else {
			Result result = new Result("请核实 Token 是否正确","",false);
			return result;
		}
	}
	@ApiOperation("获取指定用户的所有角色信息")
	@PostMapping("/GetRolesByUserIdLayUI")
	public Object findRolesByUserid(@ApiParam("用户id")@RequestParam("uId") String uId,HttpServletRequest request,@ApiParam("Token 密钥")@RequestParam("token") String token) {
		
		String sessionToken = (String) request.getSession().getAttribute("token");
		if(sessionToken==""||sessionToken==null) {
			Result result = new Result("您的Token已过期，请重新获取并登录",0,false);
			return result;
		}else if(sessionToken.equals(token)) {
			List<Roles> list = rolesService.findRolesByUserid(uId);
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("code", 0);
			map.put("count",list.size());
			map.put("data", list);
			map.put("message", 1);
			return map;
		}else {
			Result result = new Result("请核实 Token 是否正确","",false);
			return result;
		}
	}
	public String id16() {
		Date date = new Date();
	    SimpleDateFormat sdf = new SimpleDateFormat("MMddHHmmss");
	    String fd = sdf.format(date);
	    fd += String.valueOf((int)((Math.random()*9+1)*100000));
	    return fd;
	}
	
	
	
}
